1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class Solution { public: int maxDepth(TreeNode *root) { if (root == nullptr) { return 0; } TreeNode *node = root; int res = 0; queue<TreeNode *> treeQueue; treeQueue.push(node); while (!treeQueue.empty()) { res += 1; int levelSize = treeQueue.size(); for (int i = 0; i < levelSize; ++i) { node = treeQueue.front(); treeQueue.pop(); if (node->left != nullptr) { treeQueue.push(node->left); } if (node->right != nullptr) { treeQueue.push(node->right); } } } return res; } };
|